package com.wzy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzy.mapper.ArticleMapper;
import com.wzy.pojo.Article;
import com.wzy.service.ArticlesService;
import com.wzy.utils.JwtHelper;
import com.wzy.utils.R;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/article")
//获取文章数据的api
public class ArticlesController {

    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    ArticlesService articlesService;

    @Autowired
    JwtHelper jwtHelper;

    //根据ids删除
    @DeleteMapping("/deleteByIds")
    public R deleteByIds(@RequestParam("ids") List<Integer> id, HttpServletRequest request) {
        String jwt = request.getHeader("jwt");
        if(jwtHelper.parseJwt(jwt)){
            int count = 0;
            for (Integer i : id) {
                articleMapper.deleteById(i);
                count++;
            }
            if (count == id.size()) {
                System.out.println("删除成功");
                System.out.println(count);
                System.out.println(id.size());
                R ok = R.ok();
                ok.code(200);
                return ok;
            } else {
                System.out.println("删除出现异常");
                System.out.println(count);
                System.out.println(id.size());
                R error = R.error();
                error.code(501);
                return error;
            }
        }else {
            return R.error().code(300);//重定向到登陆
        }
    }


    //根据id得到内容的详细信息
    @GetMapping("/getArticle")
    public R getArticleById(@RequestParam("id") Integer id) {
            Article article = articlesService.selectById(id);
            R ok = R.ok();
            ok.data("data", article);
            return ok;
    }

    //获取文章首页列表
    @GetMapping("/getArticleList")
    public R getArticlesList(@RequestParam("pageNum") Integer pageNum) {//只接收第几页的

//        设置分页参数  第几页 页容量
        Page<Article> page = new Page<>(pageNum, 10);
        articleMapper.selectPage(page, null);
//        获取分页数据
        List<Article> list = page.getRecords();
//        list.forEach(System.out::println);
//        System.out.println("当前页：" + page.getCurrent());
//        System.out.println("每页显示的条数：" + page.getSize());
//        System.out.println("总记录数：" + page.getTotal());
//        System.out.println("总页数：" + page.getPages());
//        System.out.println("是否有上一页：" + page.hasPrevious());
//        System.out.println("是否有下一页：" + page.hasNext());
//        System.out.println("------------");
        list.forEach(a -> {
            a.setContent("");
        });//因为这里不需要内容
        R ok = R.ok();
        ok.data("data", list);
        ok.setCode(200);
        return ok;
    }

    //修改文章相关信息(不包括文章内容)
    @PostMapping("/updateArticle")
    public R editArticle(@RequestBody Article article,HttpServletRequest request) {
        String jwt = request.getHeader("jwt");
        if(jwtHelper.parseJwt(jwt)){
            //        SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String format = s1.format(new Date());
            article.setUpdatedAt(new Date());
            articleMapper.updateById(article);
            R ok = R.ok();
            ok.code(200);
            return ok;
        }else {
            return R.error().code(300);
        }
    }

    //根据id保存文章内容
    @PostMapping("/saveArticle")
    public R saveArticle(@RequestBody Article article,HttpServletRequest request) {
        String jwt = request.getHeader("jwt");
        if(jwtHelper.parseJwt(jwt)){
            article.setUpdatedAt(new Date());
            int i = articleMapper.updateById(article);
            System.out.println("---");
            System.out.println(article.getArticleId());
            return R.ok();
        }else {
            return R.error().code(300);
        }

    }

    //创建文章的相关信息
    @PostMapping("/createArticle")
    public R createArticle(@RequestBody Article article,HttpServletRequest request){
        String jwt = request.getHeader("jwt");
        if(jwtHelper.parseJwt(jwt)){
            article.setContent("默认值---无内容");
            article.setCreatedAt(new Date());
            article.setUpdatedAt(new Date());
            articleMapper.insert(article);
            R ok = R.ok();
            ok.code(200);
            return ok;
        }else {
            return R.error().code(300);
        }
    }


}
